//
//  KXDateViewController.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXDateViewController: KXViewController {
    @IBOutlet var selectBtn: KXTypeSelectBtn!
    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var bgDateView: UIView!
    @IBOutlet var datelbl: UILabel!
    var week = ["周日","周一","周二","周三","周四","周五","周六"]
    var dayweek = 0
    var dayMonth = 0
    var startStamp = 0
    var endStamp = 0
    var type : KXAddType = .pay
    var block : ((_ startStamp:Int, _ endStamp:Int)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("查看日历")
        self.selectBtn.type = self.type
        self.selectBtn.payBtn.setTitle(LS("开始日期"), for: .normal)
        self.selectBtn.inBtn.setTitle(LS("结束日期"), for: .normal)
        weak var weakself = self
        self.selectBtn.block = {type in
            weakself?.type = type
            weakself?.collectionView.reloadData()
        }
        
        self.collectionView.register(KXDateCell.nib(), forCellWithReuseIdentifier: KXDateCell.ID())
        
        self.bgDateView.layer.masksToBounds = false
        self.bgDateView.layer.shadowColor = "#9B9B9B".color().cgColor
        self.bgDateView.layer.shadowOffset = CGSize(width:0, height:0)
        self.bgDateView.layer.shadowRadius = 10
        self.bgDateView.layer.shadowOpacity = 1
        self.bgDateView.layer.cornerRadius = 10
        
        let stamp = self.stamp
        self.stamp = stamp
    }
    
    var stamp : Int = 0{
        didSet{
//            self.startStamp = stamp
//            self.endStamp = stamp
            self.dayMonth = NSDate.numOfMonth(stamp)
            let str = String(format: "%d/%d/1", self.date.year, self.date.month)
            let f = DateFormatter()
            f.timeZone = TimeZone.current
            f.dateFormat = "yyyy/MM/dd"
            let d = f.date(from: str)
            self.dayweek = NSDate.weakDay(d! as NSDate)
            self.datelbl?.text = String(format:LS("%d/%d"), self.date.month, self.date.year)
            self.collectionView?.reloadData()
        }
    }
    
    var date : NSDateComponents{
         get{
             return NSDate.dateOfStamp(self.stamp) as NSDateComponents
         }
     }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        switch sender.tag{
        case 201:
            self.stamp = self.stamp - 86400 * 30
        case 202:
            self.stamp = self.stamp + 86400 * 30;
        case 203:
            if self.endStamp == 0{
                return Toast.msg(LS("请选择结束时间"))
            }else if self.endStamp < self.startStamp{
                return Toast.msg(LS("结束时间不能早于开始时间"))
            }
            self.block?(self.startStamp, self.endStamp)
            self.navigationController?.popViewController(animated: true)
        default:
            break
        }
    }
}


//MARK: - UICollectionViewDataSource
extension KXDateViewController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  self.week.count + self.dayweek - 1 + self.dayMonth
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KXDateCell.ID(), for: indexPath) as! KXDateCell
        cell.title.text = ""
        cell.title.bgHexColor = "#FFFFFF"
        cell.title.textHexColor = "#3D3D3D"
        if indexPath.row < 7{
            cell.title.text = LS(self.week[indexPath.row])
            cell.title.textHexColor = "#797B85"
        }else if indexPath.row >= self.week.count + self.dayweek - 1{
            cell.title.text = String(format:"%d", (indexPath.row + 2 - self.week.count - self.dayweek))
            
            let day = (indexPath.row + 2 - self.week.count - self.dayweek)
            let str = String(format: "%d/%d/%d", self.date.year, self.date.month, day)
            let stamp = NSDate.tranToStamp(str, "yyyy/MM/dd").intValue()
            if (self.type == .pay && stamp == self.startStamp) || (self.type == .In  && stamp == self.endStamp){
                cell.title.bgHexColor = "#E27BC1"
                cell.title.textHexColor = "#FFFFFF"
            }
        }
        return cell
    }
    
}
//MARK: - UICollectionViewDelegate
extension KXDateViewController : UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row < self.week.count + self.dayweek - 1{
            return
        }else{
            let day = (indexPath.row + 2 - self.week.count - self.dayweek)
            let str = String(format: "%d/%d/%d", self.date.year, self.date.month, day)
            let stamp = NSDate.tranToStamp(str, "yyyy/MM/dd").intValue()
            if self.type == .pay{
                self.startStamp = stamp
            }else{
                self.endStamp = stamp
            }
            collectionView.reloadData()
        }
    }
}

//MARK: - UICollectionViewDelegateFlowLayout
extension KXDateViewController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: (kUIScreenWidth - 40) / 7, height: 40)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return .zero
    }
}
